import {
  submit as submitFun,
} from '../services/register';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';

export default {
  namespace: 'register',

  state: {
    status: undefined,
    msg: 'ERROR',
  },

  effects: {
    *submit({ payload, callback }, { call }) {
      const response = yield call(submitFun, payload);
      if(callback){
        callback(response);
      }
      /*
      yield put({
        type: 'registerHandle',
        payload: response,
      });
      */
    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority(payload.data.name);
      reloadAuthorized();
      return {
        ...state,
        status: payload.code,
        msg: payload.msg,
      };
    },
  },
};
